//
//  LFNewFeatureViewCell.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/23.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

  //MARK : -- 声明代理
protocol LFNewFeatureViewCellDelegate:NSObjectProtocol {
    //回调 点击按钮事件
    func didClickStartBtn()
}

class LFNewFeatureViewCell: UICollectionViewCell {
    
    //  //MARK : -- 声明代理属性
    weak var delegate: LFNewFeatureViewCellDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(newFeatureImage)
        contentView.addSubview(startBtn)
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
      //MARK : -- UI布局
    private func prepareUI() {
        
        newFeatureImage.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //图片布局
        contentView.addConstraint( NSLayoutConstraint(item: newFeatureImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint( NSLayoutConstraint(item: newFeatureImage, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        contentView.addConstraint( NSLayoutConstraint(item: newFeatureImage, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        contentView.addConstraint( NSLayoutConstraint(item: newFeatureImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //按钮布局
        contentView.addConstraint(  NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(  NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 200))
        contentView.addConstraint(  NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        contentView.addConstraint(  NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    
    
    //设置图片
    var index: Int = 0 {
        didSet {
          newFeatureImage.image = UIImage(named: "new_feature_\(index + 1)")
            
            //按钮加载图就hidden
            startBtn.hidden = true
        }
    }
    
    //开始按钮动画
      func startBtnAnimation() {
        //设置hidden 为 false
        
        startBtn.hidden = false
        //需要重置
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
      //MARK : -- 按钮点击事件
    func didClickStart() {
        
        delegate?.didClickStartBtn()
        
    }
  
    //MARK : -- 懒加载
    
    private lazy var newFeatureImage: UIImageView = UIImageView(image: UIImage(named:"new_feature_1"))
  
    private lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.addTarget(self, action: "didClickStart", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
}
